<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Letters sent' );

$query = "SELECT threaded_letters FROM users WHERE user_id = " . $_SESSION[ 'user_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
if( $row[ 'threaded_letters' ] == 1 ) {
	$threaded = true;
}
else {
	$threaded = false;
}

$query = "SELECT faction_id FROM faction_member WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
if( isset( $row[ 'faction_id' ] ) ) {
	$faction_id_tmp = $row[ 'faction_id' ];
}
else {
	$faction_id_tmp = -1;
}
$query = "SELECT DISTINCT messages.message_id, subject, date " .
		"FROM messages " .
		"JOIN message_ruler ON messages.message_id = message_ruler.message_id " .
		"WHERE sender = " . $_SESSION[ 'ruler_id' ] . " AND hide = 0 " .
		"ORDER BY turn DESC, timestamp DESC";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num = mysql_numrows( $result );
?>
					<div class="blok">
<?php
printDiplomacyTab( 'Letters sent' );
?> 
					</div>
					<br />
<?php
if( $num > 0 ) {
	//print "<form method=\"post\" action=\"letter_action.php\">\r\n";
	print "<table><th colspan=\"3\">" . translate( 'Letters sent' ) . ":</th>\r\n";
	print "<tr><td>" . translate( 'Subject' ) . "</td><td>" . translate( 'To' ) . "</td><td>" . translate( 'Date' ) . "</td></tr>\r\n";
	$i = 0;
	while ( $i < $num ) {
	    if( $i % 2 == 0 ) {
	        print "<tr id=\"even\">";
	    }
	    else {
	        print "<tr>";
	    }
	    
	    $query2 = "SELECT name, ruler.ruler_id FROM ruler JOIN message_ruler ON ruler.ruler_id = message_ruler.ruler_id WHERE message_id = " . mysql_result( $result, $i, "message_id" );
	    $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$num2 = mysql_numrows( $result2 );
		$i2 = 0;
		while ( $i2 < $num2 ) {
		    $query3 = "SELECT faction_id FROM faction_member WHERE ruler_id = " . mysql_result( $result2, $i2, "ruler_id" ) . " AND invited = 0 AND requested = 0";
		    $result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
			$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
			if( $_SESSION[ 'ruler_id' ] == mysql_result( $result2, $i2, "ruler_id" ) ) {
				$strong = "<strong class=\"green\">";
			}
			else if( !isset( $row3[ 'faction_id' ] ) || $faction_id_tmp != $row3[ 'faction_id' ] ) {
				$strong = "<strong class=\"red\">";
			}
			else {
				$strong = "<strong class=\"blue\">";
			}
			
			if( $i2 == 0 ) {
				$to = "<a href=\"../overview/ruler.php?name=" . mysql_result( $result2, $i2, "name" ) . "\">$strong" . mysql_result( $result2, $i2, "name" ) . "</strong></a>";
			}
			else {
				$to = $to . ", <a href=\"../overview/ruler.php?name=" . mysql_result( $result2, $i2, "name" ) . "\">$strong" . mysql_result( $result2, $i2, "name" ) . "</strong></a>";
				//$to = $to . ", " . mysql_result( $result2, $i2, "name" );
			}
			$i2 ++;
		}
		
		
		$count = getThreadCount( mysql_result( $result, $i, "message_id" ) );
		if( $count > 1 ){
			$count = " ($count)";
		}
		else {
			$count = "";
		}
	    print( "<td><a href=\"letter.php?id=" .  mysql_result( $result, $i, "message_id" ) . "#focus\">" . stripslashes( mysql_result( $result, $i, "subject" ) ) . $count . "</a></td>" .
	    		"<td>" . $to . "</td><td>" .  mysql_result( $result, $i, "date" ) );
	    print "</td></tr>\r\n";
	    $i ++;
	}
	print "</table>";
}
else {
	print( "<div class=\"blok\">\r\n" );
	print( translate( 'You have no sent letters.' ) . "<br />\r\n" );
	print( "</div>\r\n" );    
}
aos_footer();
?>
